package com.duan.goods.controller;

import com.duan.entity.PageResult;
import com.duan.entity.Result;
import com.duan.goods.pojo.Spec;
import com.duan.goods.service.SpecService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品规格
 */
@RestController
@RequestMapping("/Spec")
public class SpecController {
    @Autowired
    private SpecService specService;

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/selectAll")
    public Result<List<Spec>> selectAll(){
        return Result.ok("查询成功", specService.selectAll());
    }

    /**
     * 新增规格
     * @param spec
     * @return
     */
    @PostMapping("/insert")
    public Result insert(@RequestBody Spec spec){
        specService.insert(spec);
        return Result.ok("新增成功");
    }

    /**
     * 修改规格
     * @param spec
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Spec spec){
        specService.update(spec);
        return Result.ok("修改成功");
    }

    /**
     * 删除规格
     * @param ids
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@RequestParam String ids){
        specService.delete(ids);
        return Result.ok("删除成功");
    }

    /**
     * 规格列表条件查询
     * @param spec
     * @return
     */
    @PostMapping("/selectList")
    public Result<List<Spec>> selectList(@RequestBody Spec spec){
        return Result.ok("条件查询成功",specService.selectList(spec));
    }

    /**
     * 规格列表分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/pageAll")
    public Result pageAll(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer size){
        Page<Spec> pageList = specService.pageAll(page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }

    /**
     * 分页条件查询
     * @param page
     * @param size
     * @param spec
     * @return
     */
    @GetMapping("/pageList")
    public Result pageList(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer size,@RequestBody Spec spec){
        Page<Spec> pageList = specService.pageList(page, size, spec);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return Result.ok("查询成功",pageResult);
    }
}
